﻿/**
 * zebron
 * 登录界面
 */

class LoginPage extends UiFramework.UiAbstract {

    log: HTMLElement;
    onStart(divSelf: HTMLDivElement) {

        // 灰色框
        let grayFrame = document.createElement("div");
        grayFrame.className = "grayFrameShow";
        grayFrame.style.top = "18%";
        grayFrame.style.left = "20%";
        this.divSelf.appendChild(grayFrame);

        // 安装 ID
        let txtTitle = document.createElement("text");
        txtTitle.innerText = "安装ID";
        txtTitle.style.position = "absolute";
        txtTitle.style.top = "20%";
        txtTitle.style.left = "45%";
        txtTitle.style.height = "50%";
        this.divSelf.appendChild(txtTitle);

        // 账号输入框
        let accountFrame = document.createElement("input");
        accountFrame.type = "text";
        accountFrame.placeholder = "账号";
        accountFrame.style.position = "absolute";
        accountFrame.style.top = "25%";
        accountFrame.style.width = "54%";
        accountFrame.style.height = "20px";
        accountFrame.style.left = "23%";
        accountFrame.style.background = "#EBEBEB";
        accountFrame.style.borderColor = "#549788";
        accountFrame.className = "inputLine";
        this.divSelf.appendChild(accountFrame);

        // 密码输入框
        let pwdFrame = document.createElement("input");
        pwdFrame.type = "text";
        pwdFrame.placeholder = "密码";
        pwdFrame.style.position = "absolute";
        pwdFrame.style.top = "30%";
        pwdFrame.style.width = "54%";
        pwdFrame.style.height = "20px";
        pwdFrame.style.left = "23%";
        pwdFrame.style.background = "#EBEBEB";
        pwdFrame.style.borderColor = "#549788";
        pwdFrame.className = "inputLine";
        this.divSelf.appendChild(pwdFrame);

        // 登录按钮
        let btnLogin = document.createElement("input");
        btnLogin.type = "button";
        btnLogin.value = "登录";
        btnLogin.style.top = "40%";
        btnLogin.className = "smallGreenButton";
        this.divSelf.appendChild(btnLogin);


        // 点击跳转
        btnLogin.onclick = () => {

            // username, password, msgcode 与接口文档保持一致
            let login = {} as { url: string, username: string, password: string, msgcode: string };
            login.url = "UserLogin";
            login.username = accountFrame.value;
            login.password = pwdFrame.value;
            login.msgcode = null;

            let tsId = massageMgr.callAndroid(DataFramework.AndriodObject.HTTP, login, (json: { state: boolean, error: string, st: number, fid: string }) => {

                let aaa = document.getElementById("aaaa");
                aaa.innerText = tsId;


                if (json.state) {
                    switch (json.st) {
                        case 10000:
                            this.stateMgr.changeStateByName("configureUI");
                            break;
                        case 10006:
                            let log = {} as { v: string };
                            log.v = "密码错误";
                            massageMgr.call(massageMgr.callAndroid(DataFramework.AndriodObject.LOG_TOAST, log, null));
                            break;
                    }

                    
                }
            });
            massageMgr.call(tsId);

            // 测试跳转
            //this.stateMgr.changeStateByName("configureUI");
            //this.openDialog("bedroomWindow");
        }
    }

    // 回调方法
    loginResult(json: { state: boolean, error: string, st: number, fid: string }) {
        if (json.state) {
            switch (json.st) {
                case 10000:
                    this.stateMgr.changeStateByName("configureUI");
                    break;
                case 10006:
                    let log = {} as { v: string };
                    log.v = "密码错误";
                    massageMgr.call(massageMgr.callAndroid(DataFramework.AndriodObject.LOG_TOAST, log, null));
                    break;
            }


        }
    }

    onMessage(meassge: any) {

    }

    onEnd() {

    }

    onEvent(event: UiFramework.SmartEvent) {

    }
}

//  界面管理
UiMap["loginPage"] = () => {
    return new LoginPage();
}